package com.axin.blog.controller;

import com.axin.blog.api.domain.WebUser;
import com.axin.blog.service.IWebUserService;
import com.axin.common.core.domain.web.AjaxResult;
import com.axin.common.core.domain.web.TableDataInfo;
import com.axin.common.security.base.BaseController;
import com.axin.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 客户端用户 接口控制
 *
 * @Author axin
 * @Date 2021/10/29 21:02
 **/
@RestController
@RequestMapping("/webUser")
public class WebUserController extends BaseController {

    @Autowired
    private IWebUserService webUserService;

    /**
     * 根据参数分页查询客户端用户列表
     */
    @PreAuthorize("@ss.hasPermi('web:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(WebUser user) {
        return webUserService.selectPageWebUserList(user);
    }


    /**
     * 新增博客
     */
    @PreAuthorize("@ss.hasPermi('web:user:add')")
    @PostMapping()
    public AjaxResult add(@Validated @RequestBody WebUser user) {

        return toAjax(webUserService.insertWebUser(user));
    }


    /**
     * 根据博客ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('web:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult<WebUser> getInfo(@PathVariable Long userId) {
        return AjaxResult.success(webUserService.selectWebUserById(userId));
    }

    /**
     * 修改博客
     */
    @PreAuthorize("@ss.hasPermi('web:user:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody WebUser user) {
        return toAjax(webUserService.updateWebUser(user));
    }


    /**
     * 删除博客
     */
    @PreAuthorize("@ss.hasPermi('web:user:remove')")
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        webUserService.deleteWebUserByIds(userIds);
        return success();
    }

    /***********************************前台接口**********************************************************************/


    /**
     * 通过用户名查询客户端用户
     *
     * @param username 用户名
     * @return 用户对象信息
     */
    @GetMapping("/infoByUsername/{username}")
    public AjaxResult<WebUser> infoByUsername(@PathVariable("username") String username) {

        WebUser user = webUserService.selectUserByUserName(username);

        return AjaxResult.success(user);
    }


    @PostMapping("/register")
    public AjaxResult register(@RequestBody WebUser user) {

        return webUserService.register(user);
    }


    /**
     * 记录登录信息
     *
     * @param user
     * @return
     */
    @PostMapping("/updateUserProfile")
    public AjaxResult updateUserProfile(@RequestBody WebUser user) {

        return AjaxResult.success(webUserService.updateWebUser(user));
    }


}
